package com.ag.oa.core.service.impl;

import com.ag.oa.core.dao.IUserDao;
import com.ag.oa.core.dao.IWeeklyDao;
import com.ag.oa.core.entity.vo.WeeklyVO;
import com.ag.oa.core.service.IWeeklyService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service("weeklyServiceImpl")
public class WeeklyServiceImpl implements IWeeklyService {

    @Autowired
    private IWeeklyDao weeklyDao;

    @Autowired
    private IUserDao userDao;

    @Override
    public void getWeeklyReportForPageList(Integer userId, Integer page, JSONObject res) {

        String username = userDao.getUsernameById(userId);
        res.put("username", username);

        page = page * 15;

        List<WeeklyVO> list = weeklyDao.getWorkReportsPageData(userId, page);
        res.put("dataArrList", list);
    }

    @Override
    public void saveNewWeeklyReport(Integer userId, String startDate, String endDate, JSONObject res) {

        int code = 201;

        // 检查该段日期周报是否已存在
        int count = weeklyDao.getWeeklyReportCount(userId, startDate, endDate);
        if (count==0) {
            code = 200;
            WeeklyVO weeklyVO = new WeeklyVO();
            weeklyVO.setUserId(userId);
            weeklyVO.setStartDate(startDate);
            weeklyVO.setEndDate(endDate);
            weeklyDao.saveNewWorkReport(weeklyVO);
            res.put("weeklyInfo", weeklyVO);
        }
        res.put("resCode", code);
    }

    @Override
    public void getUserWorkContentList(Integer id, JSONObject res) {

        // 周报的任务内容
        List<Map> taskContents = weeklyDao.getWorkContentList(id);
        res.put("taskContentArr", taskContents);
    }

    @Override
    public int saveWeeklyWorkContent(Integer contentId, Integer weeklyId, Integer taskId, String contentStr) {

        int resCode = 200;

        if (contentId==null) {
            // 获取周报任务工作内容数量
            int count = weeklyDao.getWeeklyTaskContentCount(weeklyId, taskId);
            if (count==0) {
                weeklyDao.saveWorkContent(weeklyId, taskId, contentStr);
            } else {
                resCode = 201;
            }
        } else {
            weeklyDao.updateWorkContent(contentId, contentStr);
        }
        return resCode;
    }

    @Override
    public String getWeeklyWorkContent(Integer contentId) {

        return weeklyDao.getWorkContentDetail(contentId);
    }

    @Override
    public JSONArray getTaskProgressWorkContentList(Integer taskId) {

        JSONObject object = new JSONObject();

        List<Map> contentList = weeklyDao.getTaskAllWorkContentList(taskId);
        for (Map tempMap: contentList) {
            String rangeDate = tempMap.get("rangeDate").toString();

            JSONObject tempObject = new JSONObject();
            tempObject.put("username", tempMap.get("username"));
            tempObject.put("content", tempMap.get("content"));

            if (object.containsKey(rangeDate)) {
                JSONArray contentArr = object.getJSONArray(rangeDate);
                contentArr.add(tempObject);
                // 结果
                object.put(rangeDate, contentArr);
            } else {
                JSONArray contentArr = new JSONArray();
                contentArr.add(tempObject);
                // 结果
                object.put(rangeDate, contentArr);
            }
        }

        JSONArray result = new JSONArray();

        // 遍历结果
        Iterator<String> iterator = object.keySet().iterator();
        while (iterator.hasNext()) {
            String rangeDate = iterator.next();

            JSONObject temp = new JSONObject();
            temp.put("rangeDate", rangeDate);
            temp.put("contentArr", object.getJSONArray(rangeDate));

            result.add(temp);
        }
        return result;
    }

    @Override
    public void delWeeklyReport(Integer id, Integer contentId) {

        if (id!=null) {
            weeklyDao.delById(id); // 删除周报
            weeklyDao.delWorkContent(id, null); // 删除周报下的所有工作内容
        }

        if (contentId!=null) {
            weeklyDao.delWorkContent(null, contentId);  // 删除指定 id 的周报内容
        }
    }

}
